package cn.iocoder.yudao.module.asset.controller.admin.manager;

import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.asset.controller.admin.manager.vo.*;
import cn.iocoder.yudao.module.asset.dal.dataobject.manager.ManagerDO;
import cn.iocoder.yudao.module.asset.service.manager.ManagerService;

@Tag(name = "管理后台 - 资产管理")
@RestController
@RequestMapping("/asset/manager")
@Validated
public class ManagerController {

    @Resource
    private ManagerService managerService;

    @PostMapping("/create")
    @Operation(summary = "创建资产管理")
    @PreAuthorize("@ss.hasPermission('asset:manager:create')")
    public CommonResult<Integer> createManager(@Valid @RequestBody ManagerSaveReqVO createReqVO) {
        return success(managerService.createManager(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资产管理")
    @PreAuthorize("@ss.hasPermission('asset:manager:update')")
    public CommonResult<Boolean> updateManager(@Valid @RequestBody ManagerSaveReqVO updateReqVO) {
        managerService.updateManager(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资产管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('asset:manager:delete')")
    public CommonResult<Boolean> deleteManager(@RequestParam("id") Integer id) {
        managerService.deleteManager(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除资产管理")
                @PreAuthorize("@ss.hasPermission('asset:manager:delete')")
    public CommonResult<Boolean> deleteManagerList(@RequestParam("ids") List<Integer> ids) {
        managerService.deleteManagerListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资产管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('asset:manager:query')")
    public CommonResult<ManagerRespVO> getManager(@RequestParam("id") Integer id) {
        ManagerDO manager = managerService.getManager(id);
        return success(BeanUtils.toBean(manager, ManagerRespVO.class));
    }

    @GetMapping("/getDetailByBarcodeNumber")
    @Operation(summary = "获得资产管理")
    @TenantIgnore
    @PermitAll
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ManagerRespVO> getManager(@RequestParam("barcodeNumber") String barcodeNumber) {
        ManagerRespVO manager = managerService.getManagerByBarcodeNumber(barcodeNumber);
        return success(BeanUtils.toBean(manager, ManagerRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资产管理分页")
    @PreAuthorize("@ss.hasPermission('asset:manager:query')")
    public CommonResult<PageResult<ManagerRespVO>> getManagerPage(@Valid ManagerPageReqVO pageReqVO) {
        PageResult<ManagerRespVO> pageResult = managerService.getManagerPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ManagerRespVO.class));
    }

    @GetMapping("/getBarcodeNumber")
    @Operation(summary = "获得条码编码")
    public CommonResult<String> getBarcodeNumber(@RequestParam("assetType") String assetType) {
        long currentTimeMillis = System.currentTimeMillis();
        Random random = new Random(currentTimeMillis);
        int randomNumber = random.nextInt(99999999);
        String randomNumberStr = String.format("%08d", randomNumber);
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String formattedTime = now.format(formatter);
        return success(assetType+randomNumberStr);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资产管理 Excel")
    @PreAuthorize("@ss.hasPermission('asset:manager:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportManagerExcel(@Valid ManagerPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ManagerRespVO> list = managerService.getManagerPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资产管理.xls", "数据", ManagerRespVO.class,
                        BeanUtils.toBean(list, ManagerRespVO.class));
    }

}
